package com.yunhe.abnormal.repository.base;

import com.yunhe.abnormal.domain.base.AlarmLevel;
import org.springframework.data.neo4j.annotation.Query;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface AlarmLevelRepository extends Neo4jRepository<AlarmLevel, Long> {

    AlarmLevel findByCode(Long code);

    AlarmLevel findByName(String name);

    @Query("match (a:AlarmLevel) where a.activity=true return a order by a.code")
    Iterable<AlarmLevel> findAll();

    @Query("match(a:AlarmStatus)-[]->(b:AlarmLevel) where ID(a) = {abnormalId} return b limit 1")
    AlarmLevel findAlarmLevelByAlarmId(@Param("abnormalId") Long abnormalId);

    @Query("match(a:AlarmStatus{name:{alarmName}})-[]->(b:AlarmLevel) return b limit 1")
    AlarmLevel findAlarmLevelByAlarmName(@Param("alarmName") String alarmName);

    @Query("match (a:Analog)--(p:PointDataType)--(as:AlarmStatus)--(al:AlarmLevel) where a.pointNumber in {pointNumbers} return distinct " +
            "al.name as alarmLevel")
    List<String> findAlarmLevelNamesByPointNumbers(@Param("pointNumbers") List<Long> pointNumbers, @Param("stationId") Long stationId);

    @Query("match (a:Analog)--(p:PointDataType)--(as:AlarmStatus)--(al:AlarmLevel) where a.pointNumber in {pointNumbers} return distinct " +
            "al.name as alarmLevel")
    List<String> findAlarmLevelNamesByPointNumbers(@Param("pointNumbers") List<Long> pointNumbers);

    @Query("match (obj)--(a:Analog)--(p:PointDataType)--(as:AlarmStatus) where ID(obj)={objectId} return distinct a.pointNumber")
    List<Long> findAlarmPointNumbersByObjectId(@Param("objectId") Long objectId);
}
